﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using HYElectricSystemDiagram.DataStruct;
using HYElectricSystemDiagram.TMethod;
using HYView;

namespace HYElectricSystemDiagram
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdSystemDiagramHouse : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIApplication application = commandData.Application;
			Document document = application.ActiveUIDocument.Document;
			Selection selection = application.ActiveUIDocument.Selection;
			CmdViewType types = CmdViewType.FloorPlan;
			if (SwitchView.SetUsableView(types, commandData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			int num = 0;
			List<CurveLoop> userSelectedLoopsSystem = CurveloopsSelector.GetUserSelectedLoopsSystem(commandData, document, "住户配电箱", out num);
			if (userSelectedLoopsSystem == null && num == -10)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			List<ElectricBoxData> electricBoxData = GetElectricInfo.GetElectricBoxData(document, userSelectedLoopsSystem);
			if (electricBoxData.Count == 0 && userSelectedLoopsSystem == null && num == -10)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			if (electricBoxData.Count < 1)
			{
				YJKMessageBox.Information("未找到有效的控制箱，请使用“照明配电箱”族\n\r或“住宅家居配电箱”族。");
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			UserControlData2 userData = GetXmlData.GetUserData();
			List<List<LineSettingInHouse>> lineSettingInHouseInfo = GetXmlData.GetLineSettingInHouseInfo(null);
			userData.m_LineSettingInHouse = lineSettingInHouseInfo[userData.InCase];
			List<List<LineSettingOutHouse>> lineSettingOutHouseInfo = GetXmlData.GetLineSettingOutHouseInfo(null);
			userData.m_LineSettingOutHouse = lineSettingOutHouseInfo[userData.OutCase];
			return new SystemDiagramDrawingControler(commandData, electricBoxData).DrawingSystemDiagramCmd(selection, userData);
		}
	}
}
